package com.duan.boot.service.impl;

import com.duan.boot.entity.HomePage;
import com.duan.boot.entity.User;
import com.duan.boot.mapper.UserMapper;
import com.duan.boot.service.HomePageService;
import com.duan.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author duan
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private HomePageService homePageService;

    @Override
    public User selectUserById(int id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public User selectUserByName(String name) {
        if (allUserName().contains(name)){
            return userMapper.selectUserByName(name);
        }else{
            return null;
        }
    }

    @Override
    public int insertUser(Integer id) {
        return userMapper.insertUser(id);
    }

    @Override
    public boolean saveUser(User user) {
        return userMapper.saveUser(user);
    }

    @Override
    public List<String> allUserName() {
        List<String> list = new ArrayList<>();
        for (User user: userMapper.findAllUser()){
            list.add(user.getUserName());
        }
        return list;
    }

    @Override
    public List<Integer> allUserId() {
        List<Integer> list = new ArrayList<>();
        for (User user:userMapper.findAllUser()){
            list.add(user.getUserId());
        }
        return list;
    }

    @Override
    public User selectByIdOrName(Object idOrName) {
        try {
            if (allUserName().contains(idOrName.toString())){
                return selectUserByName(idOrName.toString());
            }else if (allUserId().contains(Integer.parseInt(idOrName.toString()))){
                return selectUserById(Integer.parseInt(idOrName.toString()));
            }else {
                return null;
            }
        }catch (NumberFormatException e){
            return null;
        }

    }

    @Override
    public Boolean changePassword(String formerPassword, String newPassword, String reNewPassword, Integer id) {
        User user = selectUserById(id);
        String newFormerPassword = DigestUtils.md5DigestAsHex(formerPassword.getBytes(StandardCharsets.UTF_8));
        String newNewPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes(StandardCharsets.UTF_8));
        if (!user.getUserPassword().equals(newFormerPassword)){
            throw new RuntimeException("旧密码错误，请重试");
        }
        if (!newPassword.equals(reNewPassword)){
            throw new RuntimeException("验证新密码错误，请重试");
        }
        return userMapper.changePassword(newNewPassword, id);

    }

    /*@Override
    public byte[] getUserImg(int id) {
        return userMapper.getUserImg(id).getUserImg();
    }*/

    @Override
    public String login(Integer id, String password){
        User user = selectUserById(id);
        if (user==null||!user.getUserPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8)))){
            return null;
        }else {
            return homePageService.createToken(user);
        }
    }
}
